<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 根据长度获取随机字符串
 * @param $length
 * @return null|string
 */
function getRandChar($length)
{
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;

    for ($i = 0;
         $i < $length;
         $i++) {
        $str .= $strPol[rand(0, $max)];
    }

    return $str;
}

/**
 * 获取半径范围内的经纬度
 * @param $lat
 * @param $lon
 * @param $radius
 * @return array
 */
function getAround($lat, $lon, $radius)
{
    $PI = 3.14159265;
    $latitude = $lat;
    $longitude = $lon;

    $degree = (24901 * 1609) / 360.0;
    $radiusMile = $radius;

    $dpmLat = 1 / $degree;
    $radiusLat = $dpmLat * $radiusMile;
    $minLat = $latitude - $radiusLat;
    $maxLat = $latitude + $radiusLat;

    $mpdLng = $degree * cos($latitude * ($PI / 180));
    $dpmLng = 1 / $mpdLng;
    $radiusLng = $dpmLng * $radiusMile;
    $minLng = $longitude - $radiusLng;
    $maxLng = $longitude + $radiusLng;
    return [$minLat,$maxLat,$minLng,$maxLng];
}

/**
 * 给图片补上域名
 * @param string $img
 * @param string $default
 * @return string
 */
function get_img_url($img = '', $default = '')
{
    if (!$img)
    { // 没有图片使用默认图
        $img = config('setting.img_domain') . '/static/home/images/default.png';
    } else {
        if (!preg_match('/^((https|http)?:\/\/)[^\s]+/i', $img)) // 有图片检查是否有http://
        {
            $img = config('setting.img_domain') . '/' . $img; // 没有就拼装图片地址域名
        }
    }
    return $img;
}

/**
 * 把时间转换成 几秒、几分钟、几小时、几天前
 * @param $show_time
 * @return string
 */
function time_tran($show_time) {
    $now_time = time();
    $dur = $now_time - $show_time;
    if ($dur < 0) {
        return date('Y-m-d H:i:s', $show_time);
    } else {
        if ($dur < 60) {
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    if ($dur < 259200) {//3天内
                        return floor($dur / 86400) . '天前';
                    } else {
                        return date('Y-m-d H:i:s', $show_time);
                    }
                }
            }
        }
    }
}

/**
 * 以POST方式提交数据
 * @param $url
 * @param $data
 * @return mixed
 */
function post_data($url, $data)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $res = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($res, true);

    return $res;
}

/**
 * 以GET方式提交数据
 * @param $url
 * @param bool $is_json_decode
 * @return mixed
 */
function get_data($url, $is_json_decode = true)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回

    $res = curl_exec($ch);
    curl_close($ch);
    $res = $is_json_decode ? json_decode($res, true) : $res;

    return $res;
}

/**
 * 发送微信消息模板
 * @param $data
 * @return mixed
 */
function send_wechat_tmplmsg($data) {
    $options = config('wechat');
    $wechat = new \Wechat\TPWechat($options);
    $access_token = $wechat->checkAuth();
    $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . $access_token;
    return post_data($url, $data);
}

/**
 * 获取汉字对应的首字母
 * @param $s0
 * @return null|string
 */
function getfirstchar($s0){
    $fchar = ord($s0{0});
    if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
    $s1 = iconv("UTF-8","gb2312", $s0);
    $s2 = iconv("gb2312","UTF-8", $s1);
    if($s2 == $s0){$s = $s1;}else{$s = $s0;}
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if($asc >= -20319 and $asc <= -20284) return "A";
    if($asc >= -20283 and $asc <= -19776) return "B";
    if($asc >= -19775 and $asc <= -19219) return "C";
    if($asc >= -19218 and $asc <= -18711) return "D";
    if($asc >= -18710 and $asc <= -18527) return "E";
    if($asc >= -18526 and $asc <= -18240) return "F";
    if($asc >= -18239 and $asc <= -17923) return "G";
    if($asc >= -17922 and $asc <= -17418) return "H";
    if($asc >= -17417 and $asc <= -16475) return "J";
    if($asc >= -16474 and $asc <= -16213) return "K";
    if($asc >= -16212 and $asc <= -15641) return "L";
    if($asc >= -15640 and $asc <= -15166) return "M";
    if($asc >= -15165 and $asc <= -14923) return "N";
    if($asc >= -14922 and $asc <= -14915) return "O";
    if($asc >= -14914 and $asc <= -14631) return "P";
    if($asc >= -14630 and $asc <= -14150) return "Q";
    if($asc >= -14149 and $asc <= -14091) return "R";
    if($asc >= -14090 and $asc <= -13319) return "S";
    if($asc >= -13318 and $asc <= -12839) return "T";
    if($asc >= -12838 and $asc <= -12557) return "W";
    if($asc >= -12556 and $asc <= -11848) return "X";
    if($asc >= -11847 and $asc <= -11056) return "Y";
    if($asc >= -11055 and $asc <= -10247) return "Z";
    return null;
}

/**
 * 获取订单类型
 * @param $model
 * @return mixed
 */
function get_order_type($model) {
    $order_type_array = [
        'order' => '高端定制',
        'order_buy' => '精品购买',
        'order_recharge' => '衣币充值',
        'order_buy_after_sale' => '商城售后',
        'order_after_sale' => '量体售后',
        'franchise_order' => '特许商城'
    ];
    return $order_type_array[$model];
}

/**
 * 微信模版消息
 * @param $data
 * @return mixed
 */
function send_wechat_tpl_by_status($data) {
    $param = [
        'touser' => $data['openid'],
        'template_id' => '-xYIMQmNa_GCHhQd3yaVgOA43P-LpB-q8U5_f0EXx-0',
        'url' => $data['url'],
        'data' => [
            'first' => [ 'value' => $data['first'], 'color' => '#173177'],
            'keyword1' => [ 'value' => $data['order_sn'], 'color' => '#173177'],
            'keyword2' => [ 'value' => date('Y-m-d H:i:s'), 'color' => '#173177'],
            'remark' => [ 'value' => $data['remark'], 'color' => '#173177'],
        ]
    ];
    return send_wechat_tmplmsg(json_encode($param));
}